import User.Checkjson;
import User.Userjson;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.lang.String;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "Servlet",urlPatterns = {"/login/dgut"})
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        session.setAttribute("user",null);
        response.sendRedirect("https://cas.dgut.edu.cn/logout?callback=http://localhost:8080/Login.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession httpsession=request.getSession(true);

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        StringBuffer params= new StringBuffer();
        String token=request.getParameter("token");
        try{
            params.append("token=");
            params.append(token);
            params.append("&");
            params.append("appid=javaee");
            params.append("&");
            params.append("appsecret=b3b52e43ccfd");
            params.append("&");
            params.append("userip=a");
        }catch(Exception e1) {
            e1.printStackTrace();
        }
        HttpPost httpPost= new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken"+ "?"+ params);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
        CloseableHttpResponse resp= null;
        try{
            resp= httpClient.execute(httpPost);
            if(resp.getEntity()!= null) {
                Jsonb jsonb = JsonbBuilder.create();
                String postJsonString=EntityUtils.toString(resp.getEntity());
                Checkjson check=jsonb.fromJson(postJsonString,Checkjson.class);
                StringBuffer param= new StringBuffer();
                String openid=check.getOpenid();
                String access_token=check.getAccess_token();
                try{
                    param.append("access_token=");
                    param.append(access_token);
                    param.append("&");
                    param.append("openid=");
                    param.append(openid);
                }catch(Exception e1) {
                    e1.printStackTrace();
                }
                HttpPost Post=new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo"+"?"+param);
                try{
                    resp=httpClient.execute(Post);
                    if(resp.getEntity()!=null){
                        postJsonString=EntityUtils.toString(resp.getEntity());
                        jsonb = JsonbBuilder.create();
                        Userjson user=jsonb.fromJson(postJsonString,Userjson.class);
                        httpsession.setAttribute("user",user);
                        response.sendRedirect("http://localhost:8080/Login.jsp");
                    }
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
